﻿/**
 * Created by WU on 2016/9/8.
 */
var DBPool=require('../util/DBpool');
module.exports={
        getBooks:function (index,account,callback) {
           var sql1='select pic,username,artno,title,content,DATE_FORMAT(date,"%Y-%m-%d") date,icon from t_article,t_userinfo where t_article.userno=t_userinfo.userno order by artno desc limit ?,?';
            DBPool.getConnection(function (client) {
            client.query(sql1,[(index-1)*account,account],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }

                callback(result);
                client.release();

            });
        });
    },

    get_details_books:function (_artno,callback) {
       var sql2='select pic,username,artno,title,content,CONCAT_WS("-",year(date),month(date),day(date)) date,icon from t_article,t_userinfo where t_userinfo.userno=t_article.userno&&artno=?';
        DBPool.getConnection(function (client) {
            client.query(sql2,[_artno],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
                callback(result);
                client.release();
            });
        });
    },
    comment:function(page_no,callback){
        var sql='select pic,comno,CONCAT_WS("-",year(comtime),month(comtime),day(comtime)) date,content,username,t_comment.userno user_no,beforeno from t_comment,t_userinfo where artno=?&&t_comment.userno=t_userinfo.userno';
        DBPool.getConnection(function (client) {
            client.query(sql,[page_no],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
                callback(result);
                client.release();
            });
        });
    },
    add_comment:function(user_tel,before_no,art_no,_content,comtime,callback){
        var sql_first='select userno from t_userinfo where tel=?';
        var sql="INSERT INTO t_comment(userno,beforeno,artno,content,comtime) VALUES (?,?,?,?,?)";
        DBPool.getConnection(function (client) {
            client.query(sql_first,[user_tel],function(error1,result_first){
                    if(error1){
                        console.log(error1.message);
                        return;
                    }
                client.query(sql,[result_first[0].userno,before_no,art_no,_content,comtime],function (error,result) {
                    if(error){
                        console.log(error.message);
                        return;
                    }
                    callback(result);
                    client.release();
                });
            });
        });
    },
    like_article:function(user_tel,art_no,callback){
        DBPool.getConnection(function (client) {
        sql='insert into t_like (userno,artno) VALUES ((select userno from t_userinfo where tel=?),?)';
            client.query(sql,[user_tel,art_no],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
                callback(result);
                client.release();
            });
        });
    },
    check_like_article:function(art_no,callback){
        DBPool.getConnection(function (client) {
            sql='select userno from t_like where artno=?';
            client.query(sql,[art_no],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
                callback(result);
                client.release();
            });
        });
    }
};
